/*
 * {comment of class}
 *
 * @author (C) <a href="http://www.carfield.com.hk">Carfield Yim </a>, 2004
 */
package hk.com.carfield.action;

import hk.com.carfield.core.Config;
import hk.com.carfield.core.Context;
import hk.com.carfield.core.ItemManager;
import hk.com.carfield.item.Directory;
import hk.com.carfield.item.Item;
import hk.com.carfield.util.UrlUtil;
import hk.com.carfield.web.Request;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

public final class Default implements Body {
	private Item head = null;
	private String foot = "";
	private File redirectTo;
	private File location;
	private Config config;
	public void init(Context c, Request req, File l) throws IOException {
		location = l;
		config = c.config;
		head = ItemManager.getHeadItem(c, location, req, req.getInt("pageno"));
		if (location.isDirectory()) {
			final File[] indexs = location.listFiles(config.indexFilter());
			if(indexs != null && indexs.length > 0) {
				redirectTo = indexs[0];
				return;
			}
			foot = config.getReadme(location)+Directory.slideShowJS(c, l);
		}
	}
	public  String getCaption() {
		return location.getName();
	}
	public  String getFoot() {
		return foot;
	}
	public  Item getHead() {
		return head;
	}
	public  String getRedirectTo() throws UnsupportedEncodingException {
		return redirectTo == null ? null : UrlUtil.getUrl(config, redirectTo);
	}
	public String actionCode(){
		return null;
	}

}